package com.gec.zhs.controller;

import com.aliyun.vod.upload.common.MD5Util;
import com.gec.zhs.component.RedisComponent;
import com.gec.zhs.entity.config.AppConfig;
import com.gec.zhs.entity.constants.Constants;
import com.gec.zhs.entity.dto.TokenUserInfoDto;
import com.gec.zhs.entity.vo.ResponseVO;
import com.gec.zhs.exception.BusinessException;
import com.gec.zhs.service.UserInfoService;
import com.gec.zhs.utils.StringTools;
import com.wf.captcha.ArithmeticCaptcha;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhs
 * @version 1.0
 * @date 2025-01-18
 * @description admin端用户信息
 */
@RestController
@RequestMapping("/account")
public class UserAccountController extends ABaseController{

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private AppConfig appConfig;

    @Autowired
    private RedisComponent redisComponent;

    @ApiOperation("登陆/注册的验证码")
    @RequestMapping("/checkCode")
    public ResponseVO verificationCode(){
        //验证码,图片
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100,40);
        String code = captcha.text();
        String checkCodeKey = redisComponent.saveCheckCode(code);
        //base64编码
        String checkCode = captcha.toBase64();
        Map<String, Object> map = new HashMap<>();
        map.put("checkCode",checkCode);
        map.put("checkCodeKey",checkCodeKey);
        return getSuccessResponseVO(map);
    }

    @ApiOperation("用户登陆")
    @RequestMapping(value = "/login")
    public ResponseVO login(HttpServletRequest request,
                            HttpServletResponse response,
                            String account,
                            String password,
                            String checkCodeKey,
                            String checkCode) {
        try {
            if (!checkCode.equalsIgnoreCase(redisComponent.getCheckCode(checkCodeKey))) {
                throw new BusinessException("图片验证码不正确");
            }
            if(!account.equals(appConfig.getAdminAccount())||!password.equalsIgnoreCase(StringTools.encodeByMD5(appConfig.getAdminPassword()))){
                throw new BusinessException("账号密码错误");
            }
            String token = redisComponent.saveTokenInfoForAdmin(account);
            saveToken2Cookie(response,token);
            return getSuccessResponseVO(account);
        } finally {
            redisComponent.deleteCheckCode(checkCodeKey);
            Cookie[] cookies = request.getCookies();
            //失败了清除掉cookie的token和验证码
            if (cookies != null) {
                String token = null;
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals(Constants.TOKEN_ADMIN)) {
                        token = cookie.getValue();
                    }
                }
                if (StringUtils.hasText(token)) {
                    redisComponent.cleanAdminTokenInfo(token);
                }
            }
        }
    }

    @ApiOperation("退出")
    @RequestMapping("/logout")
    public ResponseVO logout(HttpServletResponse response){
        cleanCookie(response);
        return getSuccessResponseVO(null);
    }
}
